package com.syl.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.syl.entity.Stu;
import com.syl.entity.StudentVO;
import com.syl.mapper.StuMapper;
import com.syl.service.StuService;
import com.syl.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Classname StuController
 * @Description
 * @Date 2025/8/15 20:32
 * @Created by SYL
 */
@CrossOrigin
@RestController
public class StuController {
    @Autowired
    private StuService stuService;

    /**
     * 根据id差学生
     * @param id
     * @return
     */
    @GetMapping("/Stu/{id}")
    public R findStuByid( int id){
          Stu  stu= stuService.findStuByid(id);
        System.out.println("stu = " + stu);

        if(stu==null) return R.fail() ;

        return R.success(stu);
    }

    /**
     * 根据id和名字查学生
     * @param id
     * @param name
     * @return
     */
    @PostMapping("/Stu/idandname")
    public R findStuByidAndName(@RequestParam Integer id,@RequestParam String name){
                            //past请求 前端没有通过json传参  所以用@RequestParam
                            //而且前端对应的请求 应该把参数放在 要传的参数里而不是data里

        Stu stu=stuService.findStuByidAndName(id,name);
        if(stu==null) return R.fail();
        return R.success(stu);
    }

    /**
     * 带有分页信息的查所有学生
     * @param pageNum
     * @param pageSize
     * @return
     */
    @PostMapping("/Stu/all")
    public R findAllBtu(@RequestParam int pageNum,int pageSize){

        //设置页面大小 和当前页吗
        PageHelper.startPage(pageNum,pageSize);

        ArrayList<Stu> stus= stuService.findAllBtu();

        //创建含有页面信息的 查询数据
        PageInfo<Stu> pageHelper=new PageInfo(stus);


        if(stus.size()<=0) return R.fail();
        return R.success(pageHelper);
    }

    /**
     * 根据 名字模糊查询或年龄
     * @param map
     * @return
     */
    @PostMapping("/Stu/ageangname")
    public R findStuNameAndAge(@RequestParam Map<String, Object> map){
        ArrayList<Stu> likeStuS  =stuService.findStuNameAndAge(map);
        if(likeStuS.size()<=0) return R.fail();

        return R.success(likeStuS);
    }

    /**
     * 根据对象添加学生
     * @param stu
     * @return
     */
    @GetMapping("/Stu/add")
    public R addStu(Stu stu){
        int i= stuService.addStu(stu);

        if(i==0) return R.fail();

        return R.success();
    }

    /**
     * 根据对象修改学生
     * @param stu
     * @return
     */
    @GetMapping("/Stu/update")
    public R updateStu(Stu stu){

        int i= stuService.updateStu(stu);

        if(i==0) return R.fail();


        return  R.success();
    }


    /**
     * 根据id删除学生
     * @param id
     * @return
     */
    @GetMapping("/Stu/delete")
    public R deleteById(int id){
         int i=stuService.deleteById(id);

        if(i==0) return R.fail();

        return  R.success();
    }


    /**
     * 根据id批量删除学生
     * @param ids
     * @return
     */
    @GetMapping("/Stu/deleteByids")
    public R deleteByIds( @RequestParam List<Integer> ids){
        int i=stuService.deleteByIds(ids);

        if(i==0) return R.fail();

        return  R.success();
    }

    /**
     * 查询所有学生和班级信息
     * @return
     */
    @PostMapping("/Stu/byclass")
    public R findAllStuByClass(){
        ArrayList<StudentVO>   stuByClass=  stuService.findAllStuByClass();
        if(stuByClass.size()<=0) return R.fail();

        return R.success(stuByClass);
    }

    /**
     * 根据学生id查询他学习了几个学科
     * @param sid
     * @return
     */
    @PostMapping("/Stu/bysubject")
    public R findStuBySubject(int sid){
        StudentVO studentbySubject   =  stuService.findStuBySubject(sid);
        if(studentbySubject==null) return R.fail();

        return R.success(studentbySubject);
    }

    /**
     * 根据学生id查询他学习的所有学科和老师信息
     * @param sid
     * @return
     */
    @PostMapping("/Stu/bysubjectandteacher")
    public R  findStudentAndAllSubjectAndTeacherByStuid(int sid){
        StudentVO studentVO =  stuService.findStudentAndAllSubjectAndTeacherByStuid(sid);

        System.out.println("studentVO = " + studentVO);
        if(studentVO==null) return R.fail();

        return R.success(studentVO);
    }

    /**
     * 拆分查询所有学生和班级信息
     * @return
     */
    @PostMapping("/Stu/byclassv2")
    public R findAllStuByClassV2(){
            List<StudentVO> studentVOList = stuService.findAllStuByClassV2();
        System.out.println("studentVOList = " + studentVOList);
        if(studentVOList.size()<=0) return R.fail();
        return R.success(studentVOList);
    }
    @PostMapping("/Stu/bysubjectv2")
    public R findStuBySubjectV2(int sid){
        StudentVO studentVO = stuService.findStuBySubjectV2(sid);
        System.out.println("studentVO = " + studentVO);
        if(studentVO==null) return R.fail();
        return R.success(studentVO);
    }
}
